package com.shenmazong.servereduuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.servereduuser.entity.TbAdmin;
import com.shenmazong.servereduuser.mapper.TbAdminMapper;
import com.shenmazong.servereduuser.service.TbAdminService;
import com.shenmazong.utils.ConstUtils;
import com.shenmazong.utils.Idutils;
import com.shenmazong.utils.JwtUtils;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.AdminVo;
import io.jsonwebtoken.JwtBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbAdminServiceImpl implements TbAdminService{

    @Autowired
    private TbAdminMapper tbAdminMapper;

    @Override
    public ResultResponse add(AdminVo admin) {
        //--
        TbAdmin one = tbAdminMapper.selectOne(
                new QueryWrapper<TbAdmin>().lambda()
                        .eq(TbAdmin::getAdminName, admin.getAdminName())
                        .last("limit 1")
        );
        if(one != null) {
            return ResultResponse.FAILED(400, "用户名重名");
        }

        //--2
        TbAdmin tbAdmin = new TbAdmin();
        BeanUtils.copyProperties(admin, tbAdmin);

        tbAdmin.setAdminId(Idutils.getId());
        tbAdmin.setAdminStatus(0);

        tbAdminMapper.insert(tbAdmin);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse del(AdminVo admin) {

        if(admin.getAdminId() == null || admin.getAdminId() == 0) {
            return ResultResponse.FAILED(400, "用户ID不能为空");
        }

        TbAdmin tbAdmin = tbAdminMapper.selectById(admin.getAdminId());
        if(tbAdmin == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        tbAdminMapper.deleteById(admin.getAdminId());

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse get(AdminVo admin) {
        if(admin.getAdminId() == null || admin.getAdminId() == 0) {
            return ResultResponse.FAILED(400, "用户ID不能为空");
        }

        TbAdmin tbAdmin = tbAdminMapper.selectById(admin.getAdminId());
        if(tbAdmin == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        tbAdmin.setAdminPass("");
        tbAdmin.setAdminSalt("");

        return ResultResponse.SUCCESS(tbAdmin);

    }

    @Override
    public ResultResponse set(AdminVo admin) {
        if(admin.getAdminId() == null || admin.getAdminId() == 0) {
            return ResultResponse.FAILED(400, "用户ID不能为空");
        }

        TbAdmin tbAdmin = tbAdminMapper.selectById(admin.getAdminId());
        if(tbAdmin == null) {
            return ResultResponse.FAILED(404, "用户不存在");
        }

        tbAdmin.setAdminPass(admin.getAdminPass());

        tbAdminMapper.updateById(tbAdmin);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse login(AdminVo admin) {
        //--1 检查参数
        if(admin.getAdminName() == null) {
            return ResultResponse.FAILED(401, "账号不正确");
        }
        if(admin.getAdminPass() == null) {
            return ResultResponse.FAILED(401, "密码不正确");
        }

        //--2 验证密码
        TbAdmin one = tbAdminMapper.selectOne(
                new QueryWrapper<TbAdmin>().lambda()
                        .eq(TbAdmin::getAdminName, admin.getAdminName())
                        .last("limit 1")
        );
        if(one == null) {
            return ResultResponse.FAILED(404, "账号不存在");
        }

        //--3
        if(!one.getAdminPass().equals(admin.getAdminPass())) {
            return ResultResponse.FAILED(404, "密码不正确");
        }

        admin.setAdminId(one.getAdminId());
        admin.setRealName(one.getRealName());
        admin.setAdminPass("");
        //生成token
        String token = JwtUtils.makeToken(admin.getAdminId(), admin.getAdminName(), ConstUtils.MANAGER_ROLE, admin.getRealName());
        admin.setAccessToken(token);
        return ResultResponse.SUCCESS(admin);
    }
}




